/*
 * @(#)TestCaseGenerator.java
 *
 * Revision:
 * Author                                         Date           
 * --------------------------------------------   ------------   
 *  Jeremias D. Serafim de Araujo                     06/12/2012    
 */
package br.edu.ufcg.splab.generator;

import java.util.ArrayList;

import br.edu.ufcg.splab.core.InterfaceEdge;
import br.edu.ufcg.splab.core.InterfaceGenerate;
import br.edu.ufcg.splab.core.InterfaceGraph;
import br.edu.ufcg.splab.core.InterfaceVertex;



/**
 * 
 *@see InterfaceGenerate
 *
 */

public class TestCaseGenerator implements InterfaceGenerate{


	
	/**
	 * 
	 * This function return a ArrayList of TestCase
	 * 
	 * @param graph is the model from which test cases are generated
	 * @return testSuite is ArrayList of TestCases
	 * 
	 */
	
	@Override
	public TestSuite generate(InterfaceGraph graph) {
		ArrayList<InterfaceEdge> path = new ArrayList<InterfaceEdge>();	
		TestSuite testSuite = new TestSuite();
		dfsSavePathGraph(graph.getRoot(),path,testSuite);

		return testSuite;
	}
	
	//esse metodo faz a geracao antiga no grafo com loop,
	//como na arvore nao temos loop, aproveitamos esse metodo e utilizamos
	//para pegar os caminhos de teste na arvore.
	
	
	
	/**
	 * This method use a traditional dfs to get all 
	 * paths test of a graph.
	 * 
	 * @param vertex is a root vertex
	 * @param path is an auxiliary list
	 * @param testSuite is the list with testCases
	 * 
	 */
	private void dfsSavePathGraph(InterfaceVertex vertex,ArrayList<InterfaceEdge> path, TestSuite testSuite) {

		if (vertex.isLeaf()) {
			testSuite.addTestCase(path);
		} else {
			for (InterfaceEdge edge : vertex.getOutTransitions()) {
				if (!path.contains(edge)) {
					path.add(edge);
					InterfaceVertex vertexTo = edge.getTo();
					dfsSavePathGraph(vertexTo, path, testSuite);
					path.remove(edge);
				} else {
					testSuite.addTestCase(path);
				}
			}
		}
	}

}
